<template>
  <div class="search" :style="{ '--themeColor': themeColor }">
    <nav-bar title="最近浏览" />
    <div class="shop-list">
      <mescroll-uni
        :fixed="false"
        ref="mescrollRef"
        @down="downCallback"
        @up="upCallback"
        :up="{
          auto: false,
          textNoMore: '没有更多了',
          empty: {
            tip: '暂时没有浏览历史',
            icon: 'https://hyyxboss.oss-cn-shenzhen.aliyuncs.com/img/pub/empty-business-districthistory.png',
            fixed: true,
            top: '40%',
          },
        }"
        :down="{
          auto: false,
        }"
      >
        <view v-for="item in dataList" :key="item.id" class="shop-content">
          <div class="img">
            <xh-img :src="item.logo" @click="handleToShop(item)" />
            <view
              class="hot"
              v-if="item.pubTag && Number(item.shopType) === 3"
              :style="{
                backgroundColor: themeColor ? themeColor : '#ffb72f',
              }"
            >
              <image
                src="https://hyyxboss.oss-cn-shenzhen.aliyuncs.com/img/pub/hot-business-district.png"
                class="hot-img"
              />
              热门商圈
            </view>
          </div>
          <div class="msg">
            <div class="top" @click="handleToShop(item)">
              <div class="title">
                <div class="shopName">{{ item.name }}</div>
                <div class="distance" v-if="item.distance">
                  {{ $myTool.transitionDistance(item.distance) }}
                </div>
              </div>
              <div class="detail">
                <div class="left">
                  <div :class="['falg', item.isOpen ? 'open' : '']">
                    {{ item.isOpen ? "营业中" : "休息中" }}
                  </div>
                  <div class="type">商家{{ item.shopNumbers || 0 }}</div>
                  <div class="month">月售{{ item.monthSales }}</div>
                </div>
              </div>
            </div>
            <view class="store-val coupons" v-if="item.pubCoupons">
              <view class="ticket">
                <scroll-view class="ticket_list" scroll-x="true">
                  <ticket-item
                    v-for="coupon in item.pubCoupons"
                    :key="coupon.id"
                    :denomination="coupon.denomination"
                    :discountPercent="coupon.discountPercent"
                    :transactionMinimum="coupon.transactionMinimum"
                    :receiveCode="coupon.receiveCode"
                  />
                </scroll-view>
                <view
                  class="see_all"
                  v-if="item.pubCoupons && item.pubCoupons.length > 2"
                  @click="
                    $myTool.jumpPage('businessDistrictIndexPage', {
                      shopId: item.id,
                      showTicket: true,
                    })
                  "
                >
                  更多<u-icon name="arrow-right" color="#999999" size="26rpx" />
                </view>
              </view>
            </view>
          </div>
        </view>
        <view v-if="dataList.length > 0" class="no-more">
          <view>没有更多了~</view>
          <view>仅保存近2个月的浏览历史</view>
        </view>
      </mescroll-uni>
    </div>
  </div>
</template>

<script>
import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
import { mapState } from "vuex";
import { receiveCoupon } from "@/service/api/sp-coupon";
import { getRecentlyVisitShops } from "@/service/api/sp-store/businessDistrict";
import ticketItem from "@/pages/sp-store/components/ticket-item";
export default {
  mixins: [MescrollMixin], // 使用mixin
  data() {
    return {
      dataList: [],
      queryParams: {
        latitude: "",
        longitude: "",
        shopName: "",
        sortType: 1, // 排序规则 1综合排序 2距离优先 3人气优先
        pageNum: 1,
        pageSize: 10,
      },
      defaultList: [],
    };
  },
  components: {
    ticketItem,
  },
  computed: {
    ...mapState("sp-common/base", ["themeColor"]),
  },
  onReady() {
    this.getUserLocation();
  },
  mounted() {
    this.fetData();
  },
  methods: {
    setSearch(item) {
      this.queryParams.shopName = item;
      this.fetData();
    },
    changeSelect(val) {
      this.queryParams.shopType = val.key1;
      this.queryParams.sortType = val.key2;
      this.mescroll?.resetUpScroll();
    },
    fetData() {
      let {
        queryParams,
        queryParams: { pageNum },
      } = this;
      getRecentlyVisitShops(queryParams)
        .then((res) => {
          let total = 0,
            list = [];
          if (res) {
            total = res?.total;
            list = res?.list;
          }
          if (!list) list = [];
          if (pageNum === 1) this.dataList = [];
          this.mescroll.endBySize(list.length || 0, total);
          this.dataList =
            pageNum === 1 ? list || [] : this.dataList.concat(list);
        })
        .catch(() => {
          this.mescroll?.endErr();
        });
    },
    downCallback(page) {
      this.queryParams.pageNum = 1;
      this.mescroll.resetUpScroll();
    },
    upCallback(page) {
      this.queryParams.pageNum = page.num;
      this.fetData();
    },
    getUserLocation() {
      let {
        queryParams: { longitude, latitude },
      } = this;
      if (!longitude || !latitude) {
        this.$getUserLocation()
          .then((res) => {
            let result = res?.currentLocation
              ? res?.currentLocation
              : res?.location;
            let { longitude, latitude } = result;
            this.queryParams = Object.assign({}, this.queryParams, {
              longitude,
              latitude,
            });
            if (this.queryParams.shopType || this.defaultList.length > 0) {
              this.mescroll.resetUpScroll();
            }
          })
          .catch(() => {
            this.mescroll.endErr();
          });
        return;
      }
    },
    handleToShop(item, showTicket = false) {
      if (item.pubTag && Number(item.shopType) === 3) {
        uni.navigateTo({
          url: `pages/sp-store/pages/business-district-index-page/index?shopId=${item.id}&showTicket=${showTicket}`,
        });
      } else {
        uni.navigateTo({
          url: `pages/sp-store/pages/shop-index/index?shopId=${item.id}`,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
page {
  width: 100%;
  padding-bottom: 120rpx;
  padding-top: 65px;
  background-color: #fff;
}

.search {
  //   width: 100%;
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #fff;

  .head {
    background: #fff;
    width: 100%;
    padding-top: 10rpx;

    .input {
      display: flex;
      align-items: center;
      height: 80rpx;
      // padding: 0 30rpx;
      margin: 0 auto;
      margin-bottom: 10rpx;
      margin-top: 8rpx;
      width: calc(100% - 60rpx);
      box-shadow: 0px 0px 4rpx 0px rgba(34, 34, 34, 0.14);
      border-radius: 40rpx;
      font-size: 29rpx;

      ::v-deep .u-search {
        padding: 0 30rpx;

        .u-search__content {
          padding: 0 !important;
        }
        ::v-deep .u-search__content__close {
          width: 30rpx;
          height: 30rpx;
          transform: scale(1) !important;
          -webkit-transform: scale(1) !important;
        }
        .u-icon__icon {
          // color: #888888 !important;
        }
        .u-search__action {
          font-size: 29rpx;
          font-weight: 500;
          color: var(--theme-color) !important;
          padding-left: 50rpx;
          position: relative;

          &::after {
            width: 1px;
            height: 35rpx;
            background: #dddddd;
            content: "";
            position: absolute;
            left: 24rpx;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }
  }

  .shop-list {
    flex: 1;
    overflow-y: scroll;
    position: relative;
    width: calc(100% - 40rpx);
    margin: 20rpx auto;
    // background-color: #fff;

    .shop-content {
      display: flex;
      padding: 23rpx 23rpx 26rpx 23rpx;
      margin: 0 auto 22rpx auto;
      background-color: #fff;
      border-radius: 18rpx;

      .img {
        position: relative;
        width: 170rpx;
        height: 170rpx;
        margin-right: 20rpx;
        .hot {
          position: absolute;
          left: 0;
          top: 0;
          color: #ffeee5;
          display: flex;
          align-items: center;
          border-radius: 15rpx 0rpx 15rpx 0rpx;
          padding: 8rpx 14rpx 8rpx 10rpx;
          font-size: 20rpx;
          .hot-img {
            width: 20rpx;
            height: 20rpx;
            margin-right: 5rpx;
          }
        }
      }

      .msg {
        flex: 1;
        width: 0;
        display: flex;
        flex-direction: column;

        // justify-content: space-between;
        .title {
          display: flex;
          justify-content: space-between;
          align-items: baseline;
          margin-bottom: 20rpx;
        }
        .shopName {
          font-size: 32rpx;
          font-weight: bold;
          color: #181818;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
        }
        .distance {
          font-size: 22rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
        }

        .detail {
          display: flex;
          align-items: center;
          font-size: 27rpx;
          font-weight: 500;
          color: #888888;
          .falg {
            width: 80rpx;
            height: 32rpx;
            text-align: center;
            line-height: 30rpx;
            background: #f7f7f7;
            border: 1rpx solid #999999;
            font-size: 22rpx;
            font-weight: 500;
            margin-right: 10rpx;
            border-radius: 5rpx;
          }

          .open {
            background: #e2f8f0;
            border: 1rpx solid #0cd38a;
            color: #0cd38b !important;
          }
          .month {
            font-size: 24rpx;
            font-family: PingFang SC;
            font-weight: 500;
            color: #999999;
          }
          .type {
            @extend .month;
            padding-right: 15rpx;
            margin-right: 15rpx;
            position: relative;
            &::after {
              content: "";
              width: 1px;
              height: 27rpx;
              background-color: #ddd;
              position: absolute;
              right: 0;
              top: 50%;
              transform: translateY(-50%);
            }
          }
        }

        .detail {
          display: flex;
          align-items: center;
          font-size: 27rpx;
          font-weight: 500;
          color: #888888;
          justify-content: space-between;

          .left {
            display: flex;
            align-items: center;

            .falg {
              width: 80rpx;
              height: 32rpx;
              text-align: center;
              line-height: 30rpx;
              background: #f7f7f7;
              border: 1rpx solid #999999;
              font-size: 22rpx;
              font-weight: 500;
              border-radius: 5rpx;
              margin-right: 10rpx;
            }

            .open {
              background: #e2f8f0;
              border: 1rpx solid #0cd38a;
              color: #0cd38b !important;
            }
            .type {
              padding-right: 15rpx;
              margin-right: 15rpx;
              position: relative;

              &::after {
                content: "";
                width: 1px;
                height: 27rpx;
                background-color: #ddd;
                position: absolute;
                right: 0;
                top: 50%;
                transform: translateY(-50%);
              }
            }
          }
        }
        .see_all {
          white-space: nowrap;
          font-size: 26rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #999999;
          display: flex;
          align-items: center;
          margin-left: 13rpx;
          //color: var(--theme-color);
        }
        .store-val {
          font-size: 29rpx;
          font-weight: 500;
          // color: #fff;
          margin-top: auto;
          .ticket {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: calc(100%);
            margin: 0 auto;
            margin-top: 24rpx;
            font-family: PingFang SC;
            .ticket_list {
              flex: 1;
              overflow: hidden;
              white-space: nowrap;
            }
          }
        }
      }
    }
    .no-more {
      margin-top: 86rpx;
      font-size: 22rpx;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-bottom: 30rpx;
      line-height: 30rpx;
    }
  }
}
</style>
